package com.example.restatr.Atcivity.SeekHouse;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.example.restatr.R;
import com.example.restatr.Tools;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

public class SeekHouseAt extends AppCompatActivity {
    private Toolbar tb;
    private TextView barTitle;
    private EditText search;
    private ListView zfzList;
    private List<String> sourceName = new ArrayList<>();//房屋简介
    private List<String> address = new ArrayList<>();//地址
    private List<String> areaSize = new ArrayList<>();//面积
    private List<String> tel = new ArrayList<>();//电话
    private List<String> price = new ArrayList<>();//价格/m²
    private List<String> houseType = new ArrayList<>();//户型
    private List<String> pic = new ArrayList<>();//图片
    private List<String> description = new ArrayList<>();//描述


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.activity_zfz_at);


        tb = findViewById(R.id.toolbar);
        barTitle = findViewById(R.id.bar_Title);
        search = findViewById(R.id.search);
        zfzList = findViewById(R.id.tccList);

        Tools.SetBarTitle( tb,barTitle,"找房子",search, SeekHouseAt.this,false );

        new Thread(new Runnable() {
            @Override
            public void run() {
                final String get = Tools.Get("/prod-api/api/house/housing/list", "");
                if (get != null) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                JSONObject jx = new JSONObject(get);
                                JSONArray rows = jx.getJSONArray("rows");
                                for (int i = 0; i < rows.length(); i++) {
                                    JSONObject all = rows.getJSONObject(i);
                                    sourceName.add(all.getString("sourceName"));
                                    address.add(all.getString("address"));
                                    areaSize.add(all.getString("areaSize"));
                                    tel.add(all.getString("tel"));
                                    price.add(all.getString("price"));
                                    houseType.add(all.getString("houseType"));
                                    pic.add(all.getString("pic"));
                                    description.add(all.getString("description"));
                                    zfzList.setAdapter(new zfzAdapter());
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });
                }
            }
        }).start();
        zfzList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(SeekHouseAt.this, HouseInfoAt.class);
                intent.putExtra("sourceName",sourceName.get(position));
                intent.putExtra("address",address.get(position));
                intent.putExtra("areaSize",areaSize.get(position));
                intent.putExtra("tel",tel.get(position));
                intent.putExtra("price",price.get(position));
                intent.putExtra("houseType",houseType.get(position));
                intent.putExtra("pic",pic.get(position));
                intent.putExtra("description",description.get(position));
                startActivity(new Intent(intent));
            }
        });
    }

    class zfzAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return sourceName.size();
        }

        @Override
        public Object getItem(int position) {
            return sourceName.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = getLayoutInflater().inflate(R.layout.zfz_list_item, null);
            ImageView tupian = convertView.findViewById(R.id.tupian);
            TextView name = convertView.findViewById(R.id.name);
            TextView diz = convertView.findViewById(R.id.diz);
            TextView phone = convertView.findViewById(R.id.phone);
            TextView mj = convertView.findViewById(R.id.mj);
            TextView hux = convertView.findViewById(R.id.hux);
            TextView jg = convertView.findViewById(R.id.jg);

            Glide.with(SeekHouseAt.this)
                    .load(Tools.ip + pic.get(position))
                    .placeholder( R.drawable.bj )
                    .error( R.drawable.bj )
                    .transform(new RoundedCorners(15))
                    .into(tupian);

            name.setText(sourceName.get(position));
            diz.setText("地址：" + address.get(position));
            phone.setText("电话：" + tel.get(position));
            mj.setText("面积：" + areaSize.get(position) + "m²");
            hux.setText("类别：" + houseType.get(position));
            jg.setText(price.get(position));


            return convertView;
        }
    }
}